package com.estock.platform.task;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

import com.estock.common.model.ESLog;
import com.estock.common.model.security.DailyTrans;
import com.estock.common.util.CommonUtil;
import com.estock.platform.manager.DBManager;
import com.estock.platform.reader.AbstractDataReader;
import com.estock.platform.reader.YahooCsvReader;
import com.estock.platform.task.support.ESJob;

/**
 * <s>it took about 2 hours at the first time it was executed.<br>
 * maybe i need bulk insert, but this task doesn't usually have so many data</s><br><br>
 * have optimized it for fast persistence.
 * 
 * @author richard
 *
 */
public class YahooCsvImporter extends ESJob {
	
	private static Logger logger = Logger.getLogger(YahooCsvImporter.class);
	
//	private static List<DailyTrans> allData = new ArrayList<DailyTrans>();
	private static int processedFileNum = 0;
	private static int processedRowNum = 0;
	
	private AbstractDataReader<DailyTrans> getDataReader() {
		return new YahooCsvReader();
	}
	
	@Override
	protected void executeJob() {
		List<String> filenames = getAllFileNames();
		for (String filename: filenames) {
			importFile(filename);
		}
		
		System.out.println("Finished importing trans data. row number: " + processedRowNum);
		logger.info("Finished importing trans data. row number: " + processedRowNum);
	}
	
	protected List<String> getAllFileNames() {
		List<String> results = new ArrayList<String>();
		String dataPath = CommonUtil.getYahooCsvDataDir();
		
		File direcory = new File(dataPath); 
		File[] files = direcory.listFiles();
		System.out.println("Start importing files in temp direcory.");
		for (File file : files) {
			results.add(file.getAbsolutePath());
		}
		
		Collections.sort(results);
		return results;
	}
	
	private void importFile(String fileName) {
		try {
			List<DailyTrans> objs = getDataReader().read(fileName);
			processedFileNum++;
			System.out.println("Imported files number: " + processedFileNum);
			
			saveData(objs);
		} catch (Exception e) {
			ESLog log = new ESLog("YahooCsvImporter", "Error in reading and writing file: " + fileName);
			DBManager.getInstance().saveOrUpdate(log);
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void saveData(List<DailyTrans> objs) {
		DBManager.getInstance().bulkSaveOrUpdate((List) objs);
		processedRowNum += objs.size();
		
		System.out.println("Imported data row number: " + processedRowNum);
	}
	
	public static void main(String[] args) {
		CommonUtil.isUnitTest = true;
		YahooCsvImporter instance = new YahooCsvImporter();	
		instance.executeJob();
	}

}
